<template>
  <el-dialog :title="'收款单详情'" :model-value="dialog" @close="close" width="50%">
    <div v-if="loading" class="loading-container">
      <el-loading :model="loading" text="加载中..." />
    </div>
    <div v-else-if="!detailData" class="empty-container">
      <el-empty description="暂无数据" />
    </div>
    <div v-else class="detail-container">
      <!-- 结算信息 -->
      <el-card class="mb-4">
        <el-descriptions border :column="2">
          <el-descriptions-item label="结算单编号">{{ detailData.settlementNumber || '-' }}</el-descriptions-item>
          <el-descriptions-item label="结算状态">{{ settlementStatusMap.get(detailData.settlementStatus)
          }}</el-descriptions-item>
          <el-descriptions-item label="结算月份">{{ detailData.settlementMonth }}</el-descriptions-item>
          <el-descriptions-item label="结算单生成日期">
            {{ formatTime(detailData.settlementGenerateDate) }}</el-descriptions-item>
          <el-descriptions-item label="结算周期开始日期">
            {{ formatTime(detailData.settlementPeriodStart) }}</el-descriptions-item>
          <el-descriptions-item label="结算周期结束日期">
            {{ formatTime(detailData.settlementPeriodEnd) }}
          </el-descriptions-item>
          <el-descriptions-item label="应收总金额">
            {{ detailData.totalReceivableAmount }}
          </el-descriptions-item>
          <el-descriptions-item label="已收金额">{{ detailData.receivedAmount }}</el-descriptions-item>
          <el-descriptions-item label="未收金额">{{ detailData.unreceivedAmount }}</el-descriptions-item>
          <el-descriptions-item label="收款状态">
            {{ receivedTypeMap.get(detailData.receivedType) }}
          </el-descriptions-item>
          <el-descriptions-item label="开票金额">{{ detailData.invoiceAmount || '-' }}</el-descriptions-item>
          <el-descriptions-item label="开票状态">{{ invoiceStatusMap.get(detailData.invoiceStatus) || '-'
            }}</el-descriptions-item>
          <el-descriptions-item label="发票号码">{{ detailData.invoiceNumber || '-' }}</el-descriptions-item>
          <el-descriptions-item label="发票抬头">{{ detailData.invoiceTitle || '-' }}</el-descriptions-item>
          <el-descriptions-item label="发票税号">{{ detailData.invoiceTaxNumber || '-' }}</el-descriptions-item>
          <el-descriptions-item label="发票类型">{{ invoiceTypeMap.get(detailData.invoiceType) || '-'
            }}</el-descriptions-item>
          <el-descriptions-item label="开票时间">{{ formatTime(detailData.invoiceTime) }}</el-descriptions-item>
          <el-descriptions-item label="平台服务费">{{ detailData.platformServiceFee || '-' }}</el-descriptions-item>
          <el-descriptions-item label="平台服务费费率">{{ detailData.platformServiceFeeRate || '-' }}</el-descriptions-item>
          <el-descriptions-item label="税费金额">{{ detailData.taxAmount || '-' }}</el-descriptions-item>
          <el-descriptions-item label="税率">{{ detailData.taxRate || '-' }}</el-descriptions-item>
          <el-descriptions-item label="其他费用">{{ detailData.otherFees || '-' }}</el-descriptions-item>
          <el-descriptions-item label="其他费用备注">{{ detailData.otherFeesRemark || '-' }}</el-descriptions-item>
          <el-descriptions-item label="隶属集团">{{ detailData.affiliatedGroup || '-' }}</el-descriptions-item>
          <el-descriptions-item label="使用车队">{{ detailData.useCarGroup || '-' }}</el-descriptions-item>
          <el-descriptions-item label="合作方确认人名称">{{ detailData.partnerConfirmUser || '-' }}</el-descriptions-item>
          <el-descriptions-item label="合作方确认时间">{{ formatTime(detailData.partnerConfirmTime) }}</el-descriptions-item>
          <el-descriptions-item label="确认时间">{{ formatTime(detailData.confirmTime) }}</el-descriptions-item>
          <el-descriptions-item label="确认人名称">{{ detailData.confirmUserName || '-' }}</el-descriptions-item>
          <el-descriptions-item label="结算备注">{{ detailData.remark }}</el-descriptions-item>
        </el-descriptions>
      </el-card>
    </div>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { formatTime } from '@/utils'
import { useUserStore } from '@/stores/user'
import { querySettlementDetail } from '@/api/settlement'
import { settlementStatusMap, receivedTypeMap, invoiceTypeMap, invoiceStatusMap } from '@/config/constants'

const props = defineProps<{
  dialog: boolean
  Id: string
}>()

const emit = defineEmits<{
  'update:dialog': [value: boolean]
  'update:currentForm': [value: object]
}>()

const userStore = useUserStore()
const loading = ref(false)
const detailData = ref<any>(null)
const driverInfo = ref<any>(null)

const close = () => {
  emit('update:dialog', false)
  // 只关闭弹窗并清理内部数据，不再清空父组件的currentForm
  detailData.value = null
}

const loadDetail = () => {
  console.log(11)
  if (!props.Id) {
    return
  }

  loading.value = true
  querySettlementDetail({ settlementUuid: props.Id }).then((res: any) => {
    if (res.code == 200) {
      detailData.value = res.data
    }
  }).catch(() => {
    ElMessage.error('获取订单详情失败')
  }).finally(() => {
    loading.value = false
  })
}

onMounted(() => {
  if (props.dialog && props.Id) {
    loadDetail()
  }
})
</script>

<style scoped>
.detail-container {
  max-height: 400px;
  overflow-y: auto;
}

.mb-4 {
  margin-bottom: 16px;
}

.card-header {
  font-weight: bold;
}

.loading-container,
.empty-container {
  min-height: 400px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 让el-descriptions组件的标签列和内容列等宽 */
/* :deep(.el-descriptions__label) {
  width: 50% !important;
  flex: 0 0 50% !important;
} */

:deep(.el-descriptions__content) {
  width: 30% !important;
  flex: 0 0 50% !important;
}
</style>
